package com.itheima.LockTest;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.Semaphore;

/**
 * @PackageName: com.itheima.LockTest
 * @ClassName: SemaphoreTest
 * @Author: Renyz
 * @Date: 2021/11/12 16:04
 * @Description: //TODO
 */


public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                //抢占资源
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"拿到了");
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    //释放资源
                    System.out.println(Thread.currentThread().getName()+"我走了");
                    semaphore.release();
                }

            }).start();
        }
    }
}
